package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ZjSysProject;
import com.ruoyi.system.domain.bo.ZjSysProjectBo;
import com.ruoyi.system.domain.vo.ZjSysProjectVo;
import com.ruoyi.system.mapper.ZjSysProjectMapper;
import com.ruoyi.system.service.IZjSysProjectService;
import com.ruoyi.zhgd.util.PageUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 项目Service业务层处理
 *
 * @author zyb
 * @date 2023-12-08
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjSysProjectServiceImpl implements IZjSysProjectService {

    private final ZjSysProjectMapper baseMapper;

    /**
     * 查询项目
     */
    @Override
    public ZjSysProjectVo queryById(Long projectId){
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 查询项目列表
     */
    @Override
    public PageUtils queryPageList(Page page, Map<String, Object> params) {
        List<Map> productList = baseMapper.projectList(page,params);
        return new PageUtils(page.setRecords(productList));
    }

    /**
     * 查询项目列表
     */
    @Override
    public List<ZjSysProjectVo> queryList(ZjSysProjectBo bo) {
        LambdaQueryWrapper<ZjSysProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjSysProject> buildQueryWrapper(ZjSysProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjSysProject> lqw = Wrappers.lambdaQuery();

        lqw.eq(bo.getDeptId() != null, ZjSysProject::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), ZjSysProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectInfo()), ZjSysProject::getProjectInfo, bo.getProjectInfo());
        lqw.between(params.get("beginTime") != null && params.get("endTime") != null,
            ZjSysProject::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return lqw;
    }

    /**
     * 新增项目
     */
    @Override
    public Boolean insertByBo(ZjSysProjectBo bo) {
        ZjSysProject add = BeanUtil.toBean(bo, ZjSysProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改项目
     */
    @Override
    public Boolean updateByBo(ZjSysProjectBo bo) {
        ZjSysProject update = BeanUtil.toBean(bo, ZjSysProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjSysProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
